﻿using BCCommon;
using BCCommon.TMS.Driver;
using BCData.TMS.Driver.DriverCostReport;
using BCDto.Sim.Admin.Admin;
using BCDto.TMS.Driver;
using BCDto.TMS.Driver.DriverCostReport;
using BCEntity.TMS.Driver.DriverCostReport;
using BCService.TMS.Order.Order;
using BCService.Utils;
using System;

namespace BCService.TMS.Driver.DriverCostReport
{
    public class DriverCostReportService : IDriverCostReportService
    {
        private readonly IDriverCostReportData driverCostReportData;
        private readonly IOrderProcessService orderProcessService;
        private readonly IDriverService driverService;

        public DriverCostReportService(IDriverCostReportData driverCostReportData, IOrderProcessService orderProcessService, IDriverService driverService)
        {
            this.driverCostReportData = driverCostReportData;
            this.orderProcessService = orderProcessService;
            this.driverService = driverService;
        }


        public DriverCostReportDto Add(DriverDto currentDriver, DriverCostReportDtoRequestDto dto)
        {
            throw new NotImplementedException();
        }

        public DriverCostReportDto Approve(AdminDto admin, long driverCostReportId, ApproveRequestDto approveRequestDto)
        {
            if (admin.AgentRecord == null)
            {
                throw new ArgumentException("没有审核权限，需要联系总公司");
            }
            var driverCostReport = this.driverCostReportData.GetDriverCostReportAsync(driverCostReportId).GetAwaiter().GetResult();
            var driver = this.driverService.GetDriverById(driverCostReport.DriverId);
            if (driver == null)
            {
                throw new ArgumentException("司机信息不存在。");
            }
            if (admin.AgentRecord.AgentRecordId != driver.AgentRecordId)
            {
                throw new ArgumentException("该司机不属于您管辖范围，不能审核");
            }
            var entity = new DriverCostReportEntity
            {
                DriverCostReportId = driverCostReport.DriverCostReportId,
                RefuseToReason = approveRequestDto.RefuseToReason,
                Status = approveRequestDto.IsApproved ? DriverCostReportStatus.Success.ToString() : DriverCostReportStatus.Refused.ToString(),
                Approver = admin.FullName,
                ApproverId = admin.AdminId,
                ApproveTime = DateTime.Now
            };
            var result = this.driverCostReportData.CostApproveAsync(entity).GetAwaiter().GetResult();
            if (result == null)
            {
                throw new ArgumentException("费用上报审核失败。");
            }
            return result.As<DriverCostReportDto>();
        }

        public DriverCostReportDto GetByDriverCostReportId(long driverCostReportId)
        {
            throw new NotImplementedException();
        }

        public PageableList<DriverCostReportDto> GetList(long? companyId, string driverPhone, string vehicleNo, string orderNo, DriverCostReportStatus? status, DriverCostReportType? costType, DateTime? start, DateTime? end, bool? ascending, string sortName, int pageIndex, int pageSize)
        {
            throw new NotImplementedException();
        }

        public PageableList<DriverCostReportDto> GetListByDriverIdAndCostType(long driverId, DriverCostReportType? costType, int pageIndex, int pageSize)
        {
            throw new NotImplementedException();
        }
    }
}
